package ru.adhoc.truealarm.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

import ru.adhoc.truealarm.R;
import ru.adhoc.truealarm.time.TimeHelper;
import ru.adhoc.truealarm.utils.Util;

/**
 * Created by Малюгин Сергей on 14.08.13.
 */
public class CancelAlarmText extends Activity {

    private int id;
    private Context context;
    private EditText etCancelAlarmText;
    private TextView tvText;
    private TextView tvCancelAlarmHeader;
    private Button btnCancelAlarm;
    private SharedPreferences sp;
    private String myText;
    private boolean useMyText;
    private static final String KEY_PREFERENCE = "pref";
    private static final String KEY_USE_MY_TEXT = "useMyText";
    private LinearLayout llParentText;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.cancel_alarm_text);

        id = getIntent().getExtras().getInt("id");
        context = this.getApplicationContext();
        etCancelAlarmText = (EditText)findViewById(R.id.etCancelAlarmText);
        tvText = (TextView)findViewById(R.id.tvText);
        tvCancelAlarmHeader = (TextView)findViewById(R.id.tvCancelAlarmHeader);
        btnCancelAlarm = (Button)findViewById(R.id.btnCancelAlarm);

        sp = PreferenceManager.getDefaultSharedPreferences(this);
        useMyText = sp.getBoolean(KEY_USE_MY_TEXT,false);
        myText = sp.getString("myTextET", "");

        llParentText = (LinearLayout)findViewById(R.id.ll_parent_text);
        Util.getInstance(context).setTheme(llParentText);

        ifUseText();

    }

  private void ifUseText(){
      String text;
      if(useMyText)
          text = myText;
      else{
          String affirmArray[] = getResources().getStringArray(R.array.purposes);
          Random rnd = new Random();
          text = affirmArray[rnd.nextInt(affirmArray.length+1)];
      }

      if(text.length() == 0)
          tvText.setText("error");
      else
          tvText.setText(text);
  }

    //Отключает сработанный сигнал  и перезаводит будильник на следующий повтор
    private void cancelAlarm(){
        Util.getInstance(context).cancelAlarmUtil(id);
        this.finish();
    }

    public void pressCancelAlarm(View view){

        if(checkCancelAlarmText()){
            cancelAlarm();
            this.finish();
        }
        else showErrorDialog(view);
    }

    private void showErrorDialog(View view){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.tryOneMore);

        builder.setPositiveButton(R.string.btAdd, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                etCancelAlarmText.setText("");
            }
        });
        AlertDialog AD = builder.create();
        AD.show();
    }

    //Сравниваем строки и возвращаем истина/ложь
    private boolean checkCancelAlarmText(){
        if(tvText.getText().toString().equals(etCancelAlarmText.getText().toString()))
            return true;
        else
            return false;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        this.finish();
    }

    public void pressResetAlarm(View view){

        int maxRepeat = getSharedPreferences(KEY_PREFERENCE, MODE_PRIVATE).getInt("maxRepeat", 15);
        TimeHelper.getInstance(context).currentRepeat =
                TimeHelper.getInstance(context).currentRepeat + 1;

        if(TimeHelper.getInstance(context).currentRepeat <= maxRepeat){
            Util.getInstance(context).resetAlarmUtil(id);
            this.finish();
        }
        else
        {
            Toast.makeText(context, getResources()
                    .getText(R.string.maxRepeatOverflow).toString(), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Util.getInstance(context).setTheme(llParentText);
    }

}